package com.lbd.system.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.system.bean.Record;
import com.lbd.system.controller.vo.RecordVo;
import com.lbd.system.dao.RecordRepository;
import com.lbd.system.service.RecordService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by WangYajie on 2016/9/19.
 */
@Service
@Transactional
public class RecordServiceImpl extends BaseServiceImpl<Record, RecordRepository> implements RecordService {
    private final Logger log = Logger.getLogger(this.getClass());

    @Override
    public Specification<Record> getSpecification(Record message) {
        return null;
    }

    /**
     * 构建交易记录的查询条件和参数
     *
     * @param r
     * @param parameter(用户名|电话)
     * @return
     */
    @Override
    public String buildJpql(Record r, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  Record t   ");
        if (r != null) {
            RecordVo record = (RecordVo) r;
            if (record.getUser() != null) {
                if (StringUtils.isNotBlank(record.getUser().getName())) {
                    jpqlBuilder.append(" and  t.user.name like :name");
                    parameter.put("name", "%" + record.getUser().getName() + "%");
                }
                if (StringUtils.isNotBlank(record.getUser().getTelephone())) {
                    jpqlBuilder.append(" and  t.user.telephone like :telephone");
                    parameter.put("telephone", "%" + record.getUser().getTelephone() + "%");
                }
            }
            if (null != record.getType()) {
                //类别:1充值,2提现
                jpqlBuilder.append(" and  t.type = :type");
                parameter.put("type", record.getType());
            }
            if (record.getState() != null) {
                //状态1成功,2失败,3等待
                jpqlBuilder.append(" and  t.state = :state");
                parameter.put("state", record.getState());
            }
            if (StringUtils.isNotBlank(record.getTradeNo())) {
                jpqlBuilder.append(" and  t.tradeNo = :tradeNo");
                parameter.put("tradeNo", record.getTradeNo());
            }
            if (record.getApplyStartTime() != null) {
                jpqlBuilder.append(" and  createdDate >= :applyStartTime");
                parameter.put("applyStartTime", record.getApplyStartTime());
            }
            if (record.getApplyEndTime() != null) {
                jpqlBuilder.append(" and  createdDate <= :applyEndTime");
                parameter.put("applyEndTime", record.getApplyEndTime());
            }
            if (record.getFinishStartTime() != null) {
                jpqlBuilder.append(" and  finishTime >= :finishStartTime");
                parameter.put("finishStartTime", record.getFinishStartTime());
            }
            if (record.getFinishEndTime() != null) {
                jpqlBuilder.append(" and  finishTime <= :finishEndTime");
                parameter.put("finishEndTime", record.getFinishEndTime());
            }
        }
        jpqlBuilder.append(" ORDER BY t.createdDate DESC ");
        return jpqlBuilder.toString();
    }
}
